﻿using System;
using System.Collections.Generic;
using System.Linq;
using yiCommerce.Core;
using yiCommerce.Core.Data;
using yiCommerce.Core.Domain.Customers;
using yiCommerce.Core.Domain.Statuses;

namespace yiCommerce.Service.Statuses
{
    public class CommentService : ICommentService
    {

        private IRepository<Comment> _commentRep;
        private IRepository<Customer> _customerRep;
        private IRepository<Status> _statusRep;

        public CommentService(IRepository<Comment> commentRep, IRepository<Customer> customerRep, IRepository<Status> statusRep)
        {
            _commentRep = commentRep;
            _customerRep = customerRep;
            _statusRep = statusRep;
        }

        public IList<Comment> Show(int statusId)
        {
            throw new System.NotImplementedException();
        }

        public Comment GetComment(int commentId)
        {



            var query = from c in _commentRep.Table where c.Id == commentId select c;
            var p = query.SingleOrDefault();
            if (p != null)
            {
                return StatusMapper.ToComment(p);
            }
            else
            {
                return null;
            }
                        
        }

        public IList<Comment> GetPagedComments(int statusId, int pageIndex, int pageSize)
        {
            var q = from c in _commentRep.Table
                    where c.StatusId == statusId
                    orderby c.CreatedAt
                    select c;


            var tempResult = new PagedList<Comment>(q, pageIndex, pageSize);

            return StatusMapper.ToComments(tempResult.ToList(), true).ToList();
        }
        public int AddComment(int statusId, int commenterId, string text)
        {

            var customer = this._customerRep.GetById(commenterId);
            var status= this._statusRep.GetById(statusId);

            var comment = new Comment();
            comment.Commenter_Id = commenterId;
            comment.Commenter = customer;
            comment.Status= status;
            comment.Text = text;
            comment.StatusId = statusId;
            comment.CreatedAt = DateTime.Now;

            _commentRep.Insert(comment);


  
            return comment.Id;
        }
    }
}